<?php
/**
 * 订单编辑所有方法
 * 对订单编辑的显示页面与提交操作的实现
 * @author chris.zhang
 * @package ome_order_edit
 * @copyright www.shopex.cn 2011.02.25
 *
 */
class ome_order_edit{
    /**
     * 获取订单编辑时每种objtype的显示内容定义
     * @access public
     * @param int $reship_id 退货单ID
     */
    public function get_config_list(){
        $conf_list = array(
            'goods' => kernel::single("ome_order_edit_goods")->get_config(),
            'pkg' => kernel::single("ome_order_edit_pkg")->get_config(),
            'gift' => kernel::single("ome_order_edit_gift")->get_config(),
            'giftpackage' => kernel::single("ome_order_edit_giftpackage")->get_config(),
        );
        return $conf_list;
    }
    
    /**
     * 处理订单编辑时提交的数据
     * @access public
     * @param array $objtype 如：array('goods','pkg');
     * @param array $data 订单编辑的数据 //POST
     */
    public function process_order_objtype($objtype,$data){
        $obj    = array();
        $new    = array();
        $total  = 0;
        $is_order_change = false;
        $is_goods_modify = false;
        $total_divide_order_fee=0;
        if ($objtype && is_array($objtype))
        foreach ($objtype as $key =>$type){
            if ($service = kernel::service('ome.service.order.edit.'.$type)){
                if (method_exists($service,'process')){
                    $rs = $service->process($data);
                }
                
                if ($rs){
                    $obj = array_merge($obj,$rs['oobj'] ? $rs['oobj'] : array());
                    $new = array_merge($new,$rs['nobj'] ? $rs['nobj'] : array());
                    $total += is_numeric($rs['total']) ? $rs['total'] : 0;
                    $total_pmt_goods += is_numeric($rs['total_pmt_goods']) ? $rs['total_pmt_goods'] : 0;                    
                    if ($is_order_change == false) $is_order_change = $rs['is_order_change'] == true ? true : false;
                    if ($is_goods_modify == false) $is_goods_modify = $rs['is_goods_modify'] == true ? true : false;
                }
                if($type=='goods'){
                   $total_divide_order_fee=$rs['total_divide_order_fee'];
                }
            }
        }
        $rs = array(
            'obj'   => $obj,
            'new'   => $new,
            'total' => $total,
            'is_order_change' => $is_order_change,
            'is_goods_modify' => $is_goods_modify,
            'total_pmt_goods' => $total_pmt_goods,
            'total_divide_order_fee'=>$total_divide_order_fee,
        );
        return $rs;
    }

    /**
     * 校验订单编辑时提交的数据
     * @access public
     * @param array $objtype 如：array('goods','pkg');
     * @param array $data 订单编辑的数据 //POST
     */
    public function valid_order_objtype($objtype,$data){
        $flag = true;
        $msg  = '';
        if ($objtype && is_array($objtype))
        foreach ($objtype as $key =>$type){
            if ($service = kernel::service('ome.service.order.edit.'.$type)){
                if (method_exists($service,'valid')){
                    $rs = $service->valid($data);
                    if ($rs['flag'] == false){
                        $rs = array(
                            'flag' => $rs['flag'],
                            'msg' => $rs['msg'],
                        );
                        return $rs;
                    }
                }
            }
        }
        return true;
    }
    
    /**
     * 判断是否有商品数据提交
     * @param array $objtype 如：array('goods','pkg');
     * @param array $data 订单编辑的数据 //POST
     */
    public function is_null($objtype,$data){
        $flag = true;
        $dt = array();
        if ($objtype && is_array($objtype))
        foreach ($objtype as $key =>$type){
            if ($service = kernel::service('ome.service.order.edit.'.$type)){
                if (method_exists($service,'is_null')){
                    $rs = $service->is_null($data);
                    if ($rs == false) return false;
                }
            }
        }
        
        return true;
    }


    /**
     * 修改实付金额 单个就是字符串 多个数组
     * @param orderBnArr
     */
    public function updateItemObj($orderBnArr){
        if($orderBnArr){
            $omeOrderItemMdl = app::get('ome')->model('order_items');
            $omeOrderObjectMdl = app::get('ome')->model('order_objects');
            $omeOrderMdl = app::get('ome')->model('orders');
            $dataList = $omeOrderMdl->getList('order_id,order_bn,payed,pmt_order,total_amount,service_price', array('order_bn' => $orderBnArr));
              error_log(date('Y-m-d H:i:s').'订单/'.var_export(array($dataList,$orderBnArr),true),3,DATA_DIR.'/list_ERROR_ORDERS.log');

            $IdArr = array();
            foreach ($dataList as $key => $data) {
                if ($data['service_price']>0){
                    $data['total_amount'] = $data['total_amount']-$data['service_price'];
                }
                try{
                    $itemList=$omeOrderItemMdl->getList('item_id,obj_id,sale_price,sendnum,divide_order_fee',array('order_id'=>$data['order_id'],'item_type|in'=>array('product','pkg'),'delete|noequal'=>'true'));
                    if(count($itemList)==1){
                        $updateData['divide_order_fee']=$data['total_amount'];
                        $updateData['part_mjz_discount']=$data['pmt_order'];
                        $res=$omeOrderItemMdl->update($updateData,array('item_id'=>$itemList[0]['item_id']));
                        $ores=$omeOrderObjectMdl->update($updateData,array('obj_id'=>$itemList[0]['obj_id']));
                    }else{
                        $salePriceTotal=0;
                        foreach ($itemList as $j => $item) {
                            $salePriceTotal+=$item['sale_price'];
                        }

                        $divideTotal=0;
                        $i=1;
                        foreach ($itemList as $j => $item) {
                            if ($item['sendnum']>0 && $item['divide_order_fee']>0){
                                $divide_order_fee=$item['divide_order_fee'];
                            }else{
                                $divide_order_fee=round($data['total_amount']/$salePriceTotal*$item['sale_price'],2);
                            }
                            
                            $divideTotal+=$divide_order_fee;
                            if(count($itemList)==$i)
                            {
                                $divideTotal=bcmul($divideTotal,1000,0);
                                $total_amount=bcmul($data['total_amount'],1000,0);
                                if(($divideTotal-$total_amount) !=0)
                                {
                                    $differ=($divideTotal-$total_amount)/1000;
                                    $divide_order_fee=$divide_order_fee-$differ;
                                }
                            }
                            $updateData['divide_order_fee']=$divide_order_fee;
                            $updateData['part_mjz_discount']=round($item['sale_price']-$divide_order_fee,2);
                            $res=$omeOrderItemMdl->update($updateData,array('item_id'=>$item['item_id']));
                            $ores=$omeOrderObjectMdl->update($updateData,array('obj_id'=>$item['obj_id']));
                            $i++;
                        }
                        if($divideTotal !=$data['total_amount']){
                            error_log(date('Y-m-d H:i:s').'订单/'.var_export(array($data['order_bn']),true),3,DATA_DIR.'/ERROR_ORDERS.log');
                        }

                    }
                }catch(Exception $e){
                    error_log(date('Y-m-d H:i:s').'修改数据/'.var_export(array($data['order_id'],$e->getMessage()),true),3,DATA_DIR.'/update_Exception.log');
                } 
            }
        }else{
            return false;
        }

        return true;
    }

    
}